package com.py.credit.controller;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.service.ColumnService;
import com.py.credit.service.TableService;
import com.py.credit.service.XypjUserReportService;
import com.py.framework.core.annotation.IgnoreAuthority;
import com.py.framework.core.common.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 信用评价用户申请调用接口类
 *
 * @author YaoS
 * @date 18/10/23 17:03
 */
@RestController
@RequestMapping(value = "/xypjUserReport")
public class XypjUserReportController {
    @Autowired
    TableService tableService;
    @Autowired
    ColumnService columnService;
    @Autowired
    XypjUserReportService xypjUserReportService;

    /**
     * 查询系统归集侧边栏接口
     *
     * @param tableList 资源List
     * @param tybm      企业统一编码
     * @return 信息完善侧边栏数据
     * @author YaoS
     * @date 11:12 18/10/30
     **/
    @IgnoreAuthority
    @RequestMapping(value = "/selectLeftTable.json")
    public RestServiceResult selectLeftTable(
            @RequestParam(value = "tableList", defaultValue = "", required = true) String tableList,
            @RequestParam(value = "tybm", defaultValue = "", required = true) String tybm, HttpServletRequest request) {
        RestServiceResult result = new RestServiceResult<>();
        try {
            if (tableList.isEmpty()) {
                result.setMsg("查询系统归集侧边栏接口无数据！");
            } else {
                // 获取侧边栏表数据信息
                List<Map<String, Object>> list = xypjUserReportService.selectLeftTable(tableList, tybm);
                if (RestServiceResult.CODE_SUCCESS != result.getCode()) {
                    return result;
                }
                result.setData(list);
            }
            return result;
        } catch (Exception e) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("查询系统归集侧边栏接口异常！");
            ExceptionLogger.error("查询系统归集侧边栏接口异常！", e.getMessage(), e);
            return result;
        }
    }

    /**
     * 查询申请表字段详情信息
     *
     * @param tableId 资源id
     * @return RestServiceResult
     * @author YaoS
     * @date 15:14 18/10/30
     **/
    @IgnoreAuthority
    @RequestMapping(value = "/selectTableDetail.json")
    public RestServiceResult selectTableDetail(
            @RequestParam(value = "tableId", defaultValue = "", required = true) String tableId) {
        RestServiceResult result = new RestServiceResult<>();
        try {
            // 获取数据表字段信息
            List<Map<String, Object>> cloumnlist = xypjUserReportService.selectColumns(tableId);
            if (RestServiceResult.CODE_SUCCESS != result.getCode()) {
                return result;
            }
            result.setData(cloumnlist);
            return result;
        } catch (Exception e) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("查询系统归集表字段详情异常！");
            ExceptionLogger.error("查询系统归集表字段详情异常！", e.getMessage(), e);
            return result;
        }
    }

    @IgnoreAuthority
    @RequestMapping(value = "/getTableValuesByTybmAndTable.json")
    public RestServiceResult<List<Map>> getTableValuesByTybmAndTable(String tableName, String tybm) {
        RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
        List<Map> data;
        try {
            data = xypjUserReportService.getTableValuesByTybmAndTable(tableName, tybm);
            if (RestServiceResult.CODE_SUCCESS != result.getCode()) {
                return result;
            }
            result.setData(data);
            return result;
        } catch (Exception e) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("规则查询接口异常");
        }
        return result;
    }

    /**
     * 查询展示列表的数据
     *
     * @param tableName
     * @param tableId
     * @param tybm
     * @param condition
     * @return RestServiceResult
     * @author YaoS
     * @date 16:56 18/10/30
     **/
    @IgnoreAuthority
    @RequestMapping(value = "/selectTableList.json")
    public RestServiceResult<List<Map<String, Object>>> selectRecord(
            @RequestParam(value = "tableName", defaultValue = "", required = true) String tableName,
            @RequestParam(value = "tableId", defaultValue = "", required = true) int tableId,
            @RequestParam(value = "tybm", defaultValue = "", required = true) String tybm,
            @RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
            HttpServletRequest request) {
        List<String> showColumns = columnService.getColumnENByTableId(tableId);
        List<Map<String, Object>> tableList = tableService.getRecordsForXypjForPage(tybm, tableName, showColumns,
                new String[]{"record_id", "tybm"}, condition, page);
        return new RestServiceResult<>(page.getTotalCount(), tableList);
    }

    @IgnoreAuthority
    @RequestMapping(value = "/checkZmtcIsExist.json")
    public RestServiceResult<String> checkZmtcIsExist(String ztmc, String bm, String type) {
        return new RestServiceResult<>(xypjUserReportService.selectTybm(ztmc, bm, type));
    }

    @IgnoreAuthority
    @RequestMapping(value = "/getColumns.json")
    public RestServiceResult<List<HashMap<String, String>>> getColumns(
            @RequestParam(value = "tableId", defaultValue = "", required = true) int tableId) {
        return new RestServiceResult<>(columnService.getColumn(tableId));
    }

    /**
     * 根据sql查询值
     *
     * @param sql  配的sql
     * @param tybm 统一编码
     * @return 查询结果
     * @author YaoS
     * @date 18:21 18/11/4
     **/
    @RequestMapping(value = "/getValueBySql.json")
    public RestServiceResult<String> getValueBySql(
            @RequestParam(value = "sql", defaultValue = "", required = true) String sql,
            @RequestParam(value = "tybm", defaultValue = "", required = true) String tybm) {
        RestServiceResult<String> result = new RestServiceResult<>();
        if (StringUtils.isNotBlank(sql)) {
            String showColumns = columnService.getValueBySql(sql, tybm);
            result.setData(showColumns);
        }
        return result;
    }

    /**
     * 功能描述: 指标sql校验
     *
     * @param sql 校验sql
     * @return 校验结果
     * @author YaoS
     * @date 19/4/25 17:24
     */
    @IgnoreAuthority
    @RequestMapping(value = "/checkSql.json")
    public RestServiceResult<String> checkSql(
            @RequestParam(value = "sql", defaultValue = "", required = true) String sql) {
        RestServiceResult<String> result = new RestServiceResult<>();
        try {
            String s = columnService.checkSql(sql);
        } catch (Exception e) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    /**
     * 功能描述: 获取tybm
     *
     * @param mainName 主体名称
     * @return 校验结果
     * @author YaoS
     * @date 19/4/25 17:24
     */
    @IgnoreAuthority
    @RequestMapping(value = "/selectTybm.json")
    public RestServiceResult<String> selectTybm(@RequestParam(value = "mainName") String mainName) {
        String tybm = columnService.selectTybm(mainName);
        return new RestServiceResult<>(tybm);
    }
}